package cn.fyl.org.controller;

import cn.fyl.basic.exception.BusinessException;
import cn.fyl.basic.util.ExcelUtils;
import cn.fyl.basic.util.JsonResult;
import cn.fyl.basic.util.PageList;
import cn.fyl.org.domain.Shop;
import cn.fyl.org.domain.ShopLog;
import cn.fyl.org.dto.ShopDto;
import cn.fyl.org.query.ShopQuery;
import cn.fyl.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    private IShopService ShopService;

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    public List<Shop> findAll(){
        return ShopService.loadAll();
    }

    /**
     * 查询一个
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Shop findById(@PathVariable("id") Long id){
        return ShopService.loadById(id);
    }

    /**
     * 删除操作
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            ShopService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 添加和修改操作
     * @param Shop
     * @return
     */
    @PostMapping
    public JsonResult save(@RequestBody Shop Shop){
        try {
            if(Shop.getId()==null){
                ShopService.save(Shop);
            }else {
                ShopService.update(Shop);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }

    }

    /**
     * 分页查询
     * @param
     * @return
     */
    @PutMapping
    public PageList<Shop> queryPage(@RequestBody ShopQuery query){
        try {
            return ShopService.queryData(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<Shop>();
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public JsonResult patchDelete(@RequestBody List<Long> ids){
        try {
            ShopService.patchDelete(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 入驻接口
     * @param shop
     * @return
     */
    @PostMapping("/settlement")
    public JsonResult settlement(@RequestBody Shop shop){
        try {
            ShopService.settlement(shop);
            return JsonResult.me();
        } catch (BusinessException e) { // 业务异常
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            // 代码异常，或者其他异常
            return JsonResult.me().setMsg("系统异常，稍后重试");

        }

    }

    /**
     * 审核通过
     * @param log
     * @return
     */
    @PostMapping("/audit/pass")
    public JsonResult pass(@RequestBody ShopLog log){
        try {
            ShopService.pass(log);
            return JsonResult.me().setMsg("审核通过");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 驳回
     * @param log
     * @return
     */
    @PostMapping("/audit/reject")
    public JsonResult reject(@RequestBody ShopLog log){
        try {
            ShopService.reject(log);
            return JsonResult.me().setMsg("驳回成功");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("驳回失败");
        }
    }

    /**
     * excel导出
     * @param response
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response){
        try {
            List<Shop> shops = ShopService.loadAll();
            ExcelUtils.exportExcel(shops,null,"店铺信息",Shop.class,"shop.xlsx",response);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * Excel导入
     * @param file
     */
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart(value = "file",required = true) MultipartFile file){
        try {
            // 解析Excel
            List<Shop> shops = ExcelUtils.importExcel(file, 0, 1, Shop.class);
            shops.forEach(System.out::println);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @GetMapping("/getEcharts")
    public List<ShopDto> getEcharts(){
        return ShopService.getEcharts();
    }



}
